package com.study.sxc.controller;

import com.study.sxc.Entity.Usage;
import com.study.sxc.enums.ResponseCode;
import com.study.sxc.response.Response;
import com.study.sxc.service.usageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@ResponseBody
@RequestMapping("/api/usage")
public class UsageController {

    @Resource
    private usageService usageService;

    @GetMapping("/get")
    public Response<List<Usage>> getUsages() {
        List<Usage> usages;
        try {
            usages = usageService.getAllUsages();
        } catch (Exception e) {
            return Response.<List<Usage>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info("查询异常: " + e.getMessage())
                    .data(null)
                    .build();
        }
        if(usages == null || usages.isEmpty()) {
            return Response.<List<Usage>>builder()
                    .code(ResponseCode.EMPTY_RESULT.getCode())
                    .info(ResponseCode.EMPTY_RESULT.getInfo())
                    .data(usages)
                    .build();
        }
        return Response.<List<Usage>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data(usages)
                .build();
    }

    @PostMapping("/add")
    public Response<Void> addUsage(@RequestBody Usage usage) {
        // 校验必要字段
        if (usage.getMachineId() == null || usage.getUserId() == null) {
            return Response.<Void>builder()
                    .code("9999")
                    .info("机器ID和用户ID不能为空")
                    .build();
        }
        int result = usageService.addUsage(usage);
        if (result > 0) {
            return Response.<Void>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } else {
            return Response.<Void>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @PutMapping("/update")
    public Response<Void> updateUsage(@RequestBody Usage usage) {
        // 校验必要字段
        if (usage.getUsageId() == null || usage.getUsageId() <= 0) {
            return Response.<Void>builder()
                    .code("9999")
                    .info("上机记录ID不能为空且必须为正整数")
                    .build();
        }
        int result = usageService.updateUsage(usage);
        if (result > 0) {
            return Response.<Void>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } else {
            return Response.<Void>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @DeleteMapping("/delete")
    public Response<Void> deleteUsage(@RequestBody Usage usage) {
        Integer id = usage.getUsageId();
        if (id == null || id <= 0) {
            return Response.<Void>builder()
                    .code("9999")
                    .info("上机记录ID不能为空且必须为正整数")
                    .build();
        }
        int result = usageService.deleteUsage(id);
        if (result > 0) {
            return Response.<Void>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } else {
            return Response.<Void>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @GetMapping("/get/{id}")
    public Response<Usage> getUsageById(@PathVariable Integer id) {
        // 根据id获取上机记录信息
        Usage usage = usageService.getUsageById(id);
        if (usage == null) {
            return Response.<Usage>builder()
                    .code(ResponseCode.EMPTY_RESULT.getCode())
                    .info(ResponseCode.EMPTY_RESULT.getInfo())
                    .data(null)
                    .build();
        }
        return Response.<Usage>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data(usage)
                .build();
    }
} 